<?php
namespace App\Helper\OpenSearch;
/**

阿里云开发搜索
**/
use OpenSearch\Client\OpenSearchClient;
use App\Models\News\News;
use App\Models\Artists\JoinArtist;
use App\Models\Artists\Artist;
use App\Models\OpenSearchLog;
use App\Models\Auctions\Art;
use App\Models\Auctions\Auction;
use App\Models\Auctions\Zhuanchang;
use OpenSearch\Client\DocumentClient;
class Search {
	public $accessKeyId = 'LTAImQ6xL9Yo3LMe';
	public $secret = '3KZgELfR0lDYL2f4hxg8uHlVxBIqzX';
	protected $endPoint = 'http://intranet.opensearch-cn-beijing.aliyuncs.com';
	public $appName = 'zhuokearts';
	protected $suggestName = '130028893';
	protected $options = '';
	protected $debug = true;
	public $client;
	public $tableName = 'main';
	
	protected $type_id = [1=> 'news', 2=> 'artists', 3=> 'auction_art', 4=> 'auction', 5=> 'artistsku'];
	
	public function __construct()
    {
		$options = array('debug' => $this->debug);

		$this->client = new OpenSearchClient($this->accessKeyId, $this->secret, $this->endPoint, $this->options);
		
    }

    public function delete_item($item_id, $item_type)
    {
    	//数据库删除
    	$info = OpenSearchLog::where('item_id', $item_id)->where('item_type', $item_type)->first();
    	if($info)
    	{
    		$data[] = [
				'cmd' => 'delete',
				'fields' => [
					'id' => $info->id
				]
			];
			$json = json_encode($data);
			$documentClient = new DocumentClient($this->client);
			$ret = $documentClient->push($json, $this->appName, $this->tableName);
			$info->delete();
			return $ret;
			// dd($ret);
    	}

    }
	
	
	//上传新闻文档
	public function news()
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(1);
		$list = News::where('id', '>', $last_id)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list);
		if($list->isEmpty())
		{
			return false;
		}
		// $list = News::where('id', 60030)->get();
		foreach($list as $key=>$row)
		{
			$data[] = [
				'cmd' => 'ADD',
				'fields' => $this->data_news($row, $max_id+1+$key)
			];
			
		}
		
		$json = json_encode($data);
		$documentClient = new DocumentClient($this->client);
		$ret = $documentClient->push($json, $this->appName, $this->tableName);
		\Log::info(json_encode($ret));
		// dd($ret);
		return $ret;
	}
	//专场
	public function zc()
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(5);
		// dd($last_id);
		$list = Zhuanchang::where('id', '>', $last_id)->where('is_show',1)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list);
		if($list->isEmpty())
		{
			return false;
		}
		// $list = News::where('id', 60030)->get();
		foreach($list as $key=>$row)
		{
			$data[] = [
				'cmd' => 'ADD',
				'fields' => $this->data_zc($row, $max_id+1+$key)
			];
			
		}
		
		$json = json_encode($data);
		$documentClient = new DocumentClient($this->client);
		$ret = $documentClient->push($json, $this->appName, $this->tableName);
		\Log::info(json_encode($ret));
		// dd($ret);
		return $ret;
	}
	//拍卖
	public function pmh()
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(4);
		// dd($last_id);
		$list = Auction::where('id', '>', $last_id)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list);
		if($list->isEmpty())
		{
			return false;
		}
		// $list = News::where('id', 60030)->get();
		foreach($list as $key=>$row)
		{
			$data[] = [
				'cmd' => 'ADD',
				'fields' => $this->data_auction($row, $max_id+1+$key)
			];
			
		}
		
		$json = json_encode($data);
		$documentClient = new DocumentClient($this->client);
		$ret = $documentClient->push($json, $this->appName, $this->tableName);
		\Log::info(json_encode($ret));
		// dd($ret);
		return $ret;
	}
	//库艺术家
	public function artistku($rows = [])
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(5);
		$list = Artist::where('id', '>', $last_id)->where('content', '<>', ' ')->whereNotNull('content')->orderBy('id', 'asc')->limit(100)->get();
		// $list = JoinArtist::where('id', 30)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list->first()->name);
		if($list->isEmpty())
		{
			return false;
		}
		foreach($list as $key=>$row)
		{
			$data[] = [
					'cmd' => 'ADD',
					'fields' => $this->data_artistsku($row, $max_id+1)
			];
			
			// dd($ret);
			
		}
		if($data)
		{
			$json = json_encode($data);
			$documentClient = new DocumentClient($this->client);
			$ret = $documentClient->push($json, $this->appName, $this->tableName);
			return $ret;
		}
		
		return false;
		
	}
	//艺术家
	public function artist($rows = [])
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(2);
		$list = JoinArtist::where('id', '>', $last_id)->orderBy('id', 'asc')->limit(100)->get();
		// $list = JoinArtist::where('id', 30)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list->first()->name);
		if($list->isEmpty())
		{
			return false;
		}
		foreach($list as $key=>$row)
		{
			$data[] = [
					'cmd' => 'ADD',
					'fields' => $this->data_artists($row, $max_id+1)
			];
			
			// dd($ret);
			
		}
		if($data)
		{
			$json = json_encode($data);
			$documentClient = new DocumentClient($this->client);
			$ret = $documentClient->push($json, $this->appName, $this->tableName);
			return $ret;
		}
		
		return false;
		
	}
	//拍卖
	public function auction_art()
	{
		$data = [];
		$max_id = $this->get_last_open_search_log();
		$last_id = $this->get_last_open_search_log_type(3);
		// dd($last_id);
		$list = Art::where('id', '>', $last_id)->orderBy('id', 'asc')->limit(100)->get();
		// dd($list);
		if($list->isEmpty())
		{
			return false;
		}
		// $list = News::where('id', 60030)->get();
		foreach($list as $key=>$row)
		{
			$data[] = [
				'cmd' => 'ADD',
				'fields' => $this->data_art($row, $max_id+1+$key)
			];
			
		}
		
		$json = json_encode($data);
		$documentClient = new DocumentClient($this->client);
		$ret = $documentClient->push($json, $this->appName, $this->tableName);
		\Log::info(json_encode($ret));
		// dd($ret);
		return $ret;
	}
	//专场
	public function data_zc($row, $id)
	{
		$company = '';
		if($row->auction)
		{
			$company = $row->auction->title;
			if($row->auction->company)
			{
				$company .= ','.$row->auction->company->title;
			}
			
		}
		$data = [
			'id' => $id,
			'type_id' => 5,
			'title' => $row->title,
			'url' => env('AUCTION_DOMAIN').'/zc/'.$row->id.'.shtml',
			'hits' => (int)$row->onlcik,
			'author' => $company,
			'cat_id' => $row->company_id,
			'create_timestamp' => (int)strtotime($row->start_time),
			'update_timestamp' => (int)strtotime($row->start_time),
			'body' => $company,
		];
		// dd($data);
		//新增增加记录表
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 5,
				'id' => $id,
			]);
		return $data;
	}
	//组合拍卖
	public function data_auction($row, $id)
	{
		$content = $row->content;
		if(is_array($row->content))
		{
			foreach ($content as $key => $value) {
				# code...
				$content = $value.'，';
			}
		}
		$company = '';
		if($row->company)
		{
			$company = $row->company->title;
		}
		$data = [
			'id' => $id,
			'type_id' => 4,
			'title' => $row->title,
			'url' => env('AUCTION_DOMAIN').'/pmh/'.$row->id.'.shtml',
			'hits' => (int)$row->onlcik,
			'author' => $company,
			'cat_id' => $row->company_id,
			'create_timestamp' => (int)strtotime($row->preview_start_time),
			'update_timestamp' => (int)strtotime($row->start_time),
			'body' => $content,
		];
		// dd($data);
		//新增增加记录表
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 4,
				'id' => $id,
			]);
		return $data;
	}
	//组合拍品数据
	public function data_art($row, $id)
	{
		$data = [
			'id' => $id,
			'type_id' => 3,
			'title' => $row->title,
			'url' => env('AUCTION_DOMAIN').'/art/'.$row->id.'.shtml',
			'hits' => $row->onlcik,
			'cat_id' => $row->class_id,
			'create_timestamp' => (int)strtotime($row->created_at),
			'update_timestamp' => (int)strtotime($row->updated_at),
			'body' => !empty($row->content)?strip_tags($row->content):'',
		];
		// dd($data);
		//新增增加记录表
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 3,
				'id' => $id,
			]);
		return $data;
	}
	
	public function data_artistsku($row, $id)
	{
		$data = [
			'id' => $id,
			'type_id' => 5,
			'title' => $row->name,
			'url' => $row->titleurl,
			'hits' => $row->onlcik,
			'cat_id' => $row->class_id,
			'create_timestamp' => (int)strtotime($row->created_at),
			'update_timestamp' => (int)strtotime($row->updated_at),
			'body' => !empty($row->content)?strip_tags($row->content):'',
		];
		// dd($data);
		//新增增加记录表
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 5,
				'id' => $id,
			]);
		return $data;
	}
	//组合艺术家数据
	public function data_artists($row, $id)
	{
		$data = [
			'id' => $id,
			'type_id' => 2,
			'title' => $row->name,
			'url' => $row->titleurl,
			'hits' => $row->onlcik,
			'cat_id' => $row->class_id,
			'create_timestamp' => (int)strtotime($row->created_at),
			'update_timestamp' => (int)strtotime($row->updated_at),
			'body' => !empty($row->content)?strip_tags($row->content):'',
		];
		// dd($data);
		//新增增加记录表
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 2,
				'id' => $id,
			]);
		return $data;
	}
	//组合新闻数据
	public function data_news($row, $id)
	{
		$tags = '';
		if(!$row->tags->isEmpty())
		{
			foreach($row->tags as $r)
			{
				$tags .= $r->name.',';
			}
			$tags = trim($tags, ',');
		}
		$data = [
			'id' => $id,
			'type_id' => 1,
			'title' => $row->title,
			'url' => env('NEWS_DOMAIN').$row->titleurl,
			'author' => $row->author?$row->author:'',
			'hits' => $row->onlcik?$row->onlcik:'0',
			'tags' => $tags,
			'cat_id' => $row->class_id,
			'create_timestamp' => (int)strtotime($row->created_at),
			'update_timestamp' => (int)strtotime($row->updated_at),
			'body' => !empty($row->content)?strip_tags($row->content->content):'',
		];
		
		$this->insert_log([
				'item_id' => $row->id,
				'item_type' => 1,
				'id' => $id,
			]);
		return $data;
	}
	//新增增加记录表
	public function insert_log($data)
	{
		if(!OpenSearchLog::where('item_type', $data['item_type'])->where('item_id', $data['item_id'])->first())
		{
			OpenSearchLog::insert($data);
		}
	}
	//查询类型最后插入的ID
	public function get_last_open_search_log_type($type)
	{
		$info = OpenSearchLog::where('item_type', $type)->orderBy('id', 'desc')->first();
		
		if($info)return $info->item_id;
		return 0;
	}
	//查询最后插入的ID
	public function get_last_open_search_log()
	{
		$id = OpenSearchLog::max('id');
		if($id)return $id;
		return 0;
	}
	
}